package day10;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyCallable implements Callable<Integer> {
    private int i;

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    public MyCallable(int i) {
        this.i = i;
    }

    @Override
    public Integer call() throws Exception {
        int n = jieCheng(i);
        return n;
    }

    private int jieCheng(int i2) {
        if(i2 == 0 || i2 == 1) {
            return 1;
        }

        return  i2 * jieCheng(i2-1);
    }

}

/*
 * 10! 8! 5!的阶乘
 */
public class CallableDemo {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //创建阶乘对象的实例
        MyCallable m = new MyCallable(10);
        MyCallable m2 = new MyCallable(8);
        MyCallable m3 = new MyCallable(5);

        //包装计算阶乘的任务对象
        FutureTask<Integer> ft = new FutureTask<Integer>(m);
        FutureTask<Integer> ft2 = new FutureTask<Integer>(m2);
        FutureTask<Integer> ft3 = new FutureTask<Integer>(m3);

        //创建线程对象
        Thread t = new Thread(ft);
        Thread t2 = new Thread(ft2);
        Thread t3 = new Thread(ft3);

        //启动线程
        t.start();
        t2.start();
        t3.start();

        //返回打印的结果
        System.out.println(m.getI()+"! ：" + ft.get());
        System.out.println(m2.getI()+"! ：" + ft2.get());
        System.out.println(m3.getI()+"! ：" + ft3.get());

    }
}